package com.xszx.controller;



import com.xszx.entity.TDept;
import com.xszx.entity.TUser;
import com.xszx.service.TUserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@Slf4j
@Controller
@Configuration  //设置当前类为配置类
@ComponentScan("com.xszx")
public class UserController {

    @Autowired
    private TUserService userService;

    /**
     * @return
     */

    @RequestMapping("/adduser")
    @ResponseBody
    public String addUser(TUser user) {
        //判断手机与身份证号是否存在
        log.info("添加用户:{}",user);
        TUser tUser = userService.isOnly(user.getPhone(), user.getIdcard());
        if (tUser==null) {
            if(user.getAccount()==null){
                //生成账号
                for (; ; ) {
                    int[] account = new int[10];
                    for (int i = 0; i < account.length; i++) {
                        Random random = new Random();
                        account[i] = random.nextInt(10);
                    }
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < account.length; i++) {
                        sb.append(account[i]);
                    }
                    String account1 = sb.toString();
                    //判断账号是否重复
                    TUser tUser1 = userService.isAccount(account1);
                    if (tUser1 == null) {
                        //添加用户
                        user.setAccount(account1);
                        userService.addUser(user);
                        return account1;
                    }
                }
            }else{
                //判断账号是否重复
                TUser tUser1 = userService.isAccount(user.getAccount());
                if (tUser1 == null) {
                    //添加用户
                    userService.addUser(user);
                    return "1";
                }else{
                    return "2";
                }
            }

        } else {
            return null;
        }
    }


    /**用户完善信息
     *
     */

    @RequestMapping("/perfect")
    @ResponseBody
    public String perfectUser(TUser user) {
        log.info("用户完善信息：{}",user);
        userService.perfectUser(user);
        return "123";
    }


    @RequestMapping("/getUserById")
    @ResponseBody
    public TUser perfectUser(@RequestParam("account") String account) {
        return userService.getUserByAccount(account);
    }
        //判断手机与身份证号是否存在
    /**
     * 获取所有用户信息
     */
    @RequestMapping("/selectuser")
    @ResponseBody
    public List<TUser> selectUsers(){
        List<TUser> tUsers = userService.selectUser();
        log.info("查询所有用户信息：{}",tUsers);
        return  tUsers;
    }


    /**
     * 模糊查询用户所有信息
     */
    @RequestMapping("/selectUserByCheck")
    @ResponseBody
    public List<TUser>  selectUserByCheck(TUser tUser){
        List<TUser> tUsers = userService.selectUser(tUser);
        log.info("查询所有用户信息：{}",tUsers);
        return  tUsers;
    }
    /**
     * 删除用户
     */
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public int deleteUserById(@RequestParam("id") int id){
        log.info("根据id删除用户：{}",id);
        userService.deleteUserById(id);
        return 1;
    }
    /**
     * 修改用户
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public int deleteUserById(TUser user){
        userService.updateUser(user);
        return 1;
    }
    /**
     * 获取用户数量
     */
    @RequestMapping("/getUserCount")
    @ResponseBody
    public int getUserCount(){
       return userService.selectUser().size();
    }

    /**
     * 获取用户性别的占比
     * @return
     */
    @RequestMapping("/getSexCount")
    @ResponseBody
    public int[] getSexCount(){
        List<TUser> users = userService.selectUser();
        int count = users.size();
        int mancount=0;
        int womancount=0;
        for (TUser user : users) {
            if(user.getSex()!=null){
                if(user.getSex()==1){
                    mancount=mancount+1;
                }else{
                    womancount=womancount+1;
                }
            }

        }

        System.out.println(mancount);
        System.out.println(womancount);

        int[] i=new int[]{mancount*100/count,womancount*100/count};

        for (int j=0;j<i.length;j++) {
            System.out.println(i[j]);
        }

        return i;
    }

    /**
     * 转发到主屏
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "jsp/index";
    }


    /**
     * 登录
     * @param account
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        HttpServletRequest request
    ) {
        try {
            log.info("账号密码登录：{},{}",account,password);
            TUser userByAccount = userService.getUserByAccount(account);
            TDept dept = userService.getUserDeptByDid(userByAccount.getDid());
            TUser tUser = new TUser();
            tUser.setAccount(account);
            tUser.setPassword(password);
            log.info("用户信息：{}",tUser);
            int loginNum = userService.ifUnameAndPwd(tUser);
            log.info("账号密码是否匹配，匹配1：{}",loginNum);
            if (loginNum == 1) {
                request.setAttribute("loginNum", loginNum);
                HttpSession session = request.getSession();
                session.setAttribute("user", userByAccount);
                session.setAttribute("deptName", dept.getDname());


                request.getSession().setAttribute("username", userByAccount.getUsername());
                return "redirect:index";
            }
            return "jsp/index";
        } catch (NullPointerException exception) {
            int loginNum = 4;
            request.setAttribute("loginNum", loginNum);
            return "jsp/login";
        }

    }


}
